package com.bsb.biz.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bsb.base.common.util.DateUtils;
import com.bsb.base.common.util.ExportExcel;
import com.bsb.base.common.util.FileUtil;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbApplyCompetition;
import com.bsb.base.model.BsbApplyCompetitionExample;
import com.bsb.base.model.BsbUser;
import com.bsb.base.model.PoiCard;
import com.bsb.biz.service.BsbApplyCompetitionService;
import com.bsb.biz.service.BsbUserService;

@Controller
@RequestMapping("/bsbApplyCompetition")
public class BsbApplyCompetitionController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbApplyCompetition/list.do";
	
	@Resource
	private BsbApplyCompetitionService bsbApplyCompetitionService;
	@Resource
	private BsbUserService bsbUserService;
	
	@RequiresPermissions("bsbApplyCompetition:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbApplyCompetitionExample bsbApplyCompetitionExample = new BsbApplyCompetitionExample();
		int totalCount = bsbApplyCompetitionService.countBsbApplyCompetition(bsbApplyCompetitionExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbApplyCompetitionExample.setPageIndex((pageNum - 1) * pageSize);
		bsbApplyCompetitionExample.setPageSize(pageSize);
		bsbApplyCompetitionExample.setOrderByClause("insertTime DESC");
		List<BsbApplyCompetition> bsbApplyCompetitions = bsbApplyCompetitionService.getPageBsbApplyCompetition(bsbApplyCompetitionExample);
		for (BsbApplyCompetition bsbApplyCompetition : bsbApplyCompetitions) {
			String userId = bsbApplyCompetition.getUserId();
			if(StringUtils.isNotEmpty(userId)){
				BsbUser bsbUserByUuid = bsbUserService.getBsbUserByUuid(userId);
				if(null != bsbUserByUuid){
					bsbApplyCompetition.setUserId(bsbUserByUuid.getPhone());
				}
			}
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbApplyCompetitions", bsbApplyCompetitions);
		return "/BsbApplyCompetition/list";
	}
	
	
	@RequiresPermissions("bsbApplyCompetition:export")
	@RequestMapping(value = "/export", method = RequestMethod.GET)
	public void export(HttpServletRequest request, HttpServletResponse response){
		BsbApplyCompetitionExample bsbApplyCompetitionExample = new BsbApplyCompetitionExample();
		bsbApplyCompetitionExample.setOrderByClause("insertTime DESC");
		List<BsbApplyCompetition> dataset = bsbApplyCompetitionService.selectByExample(bsbApplyCompetitionExample);
		for (BsbApplyCompetition bsbApplyCompetition : dataset) {
			String userId = bsbApplyCompetition.getUserId();
			if(StringUtils.isNotEmpty(userId)){
				BsbUser bsbUserByUuid = bsbUserService.getBsbUserByUuid(userId);
				if(null != bsbUserByUuid){
					bsbApplyCompetition.setUserId(bsbUserByUuid.getPhone());
				}
			}
		}
		ExportExcel<BsbApplyCompetition> ex = new ExportExcel<>();
		String[] headers = new String[] { "身份证号", "姓名", "手机号","开始观看时间","观看时长（分钟）" };
		String dir = "/home/images/poi/";
		try {
			String title = "技能大赛培训视频观看时长";
			File file = new File(dir);
			if (file.exists()) {
				File[] files = file.listFiles();
				File temp = null;
				for (int i = 0; i < files.length; i++) {
					if (files[i].isFile()) {
						temp = new File(dir + files[i].getName());
						if (temp.exists() && temp.isFile()) {
							temp.delete();
						}

					}
				}
			}
			OutputStream out = new FileOutputStream(dir + title);
			ex.exportExcel(title, headers, dataset, out, "yyyy-MM-dd");
			FileUtil.downloadFile(request, response, dir + title, title + ".xls");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	
	@RequiresPermissions("bsbApplyCompetition:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/BsbApplyCompetition/add";
	}

	@RequiresPermissions("bsbApplyCompetition:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbApplyCompetition") BsbApplyCompetition bsbApplyCompetition) {
		bsbApplyCompetitionService.addBsbApplyCompetition(bsbApplyCompetition);
		return redirect;
	}
	
	@RequiresPermissions("bsbApplyCompetition:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String del(Model model,
			@PathVariable("id") String id) {
		bsbApplyCompetitionService.delBsbApplyCompetitionById(id);
		return redirect;
	}
	
	@RequiresPermissions("bsbApplyCompetition:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toEdit(Model model,
			@PathVariable("id") String id) {
		BsbApplyCompetition bsbApplyCompetition = bsbApplyCompetitionService.getBsbApplyCompetitionById(id);
		model.addAttribute("bsbApplyCompetition", bsbApplyCompetition);
		return "/BsbApplyCompetition/edit";
	}

	@RequiresPermissions("bsbApplyCompetition:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model, @ModelAttribute("bsbApplyCompetition") BsbApplyCompetition bsbApplyCompetition) {
		bsbApplyCompetitionService.updateBsbApplyCompetition(bsbApplyCompetition);
		return redirect;
	}
	
	@RequiresPermissions("bsbApplyCompetition:view")
	@RequestMapping(value = "/{id}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("id") String id) {
		BsbApplyCompetition bsbApplyCompetition = bsbApplyCompetitionService.getBsbApplyCompetitionById(id);
		model.addAttribute("bsbApplyCompetition", bsbApplyCompetition);
		return "/BsbApplyCompetition/view";
	}
}
